var del_title = '';
window.setValue = function (name, value){
	  var first = name.substr(0,1), input, i = 0, val;
	  if(value === "") return;
	  if("#" === first || "." === first){
		  input = $(name);
	  } else {
		  input = $("[name='" + name + "']");
	  }
	  
	  if(input.eq(0).is(":radio")) { //单选按钮
		  input.filter("[value='" + value + "']").each(function(){this.checked = true});
	  } else if(input.eq(0).is(":checkbox")) { //复选框
		  if(!$.isArray(value)){
			  val = new Array();
			  val[0] = value;
		  } else {
			  val = value;
		  }
		  for(i = 0, len = val.length; i < len; i++){
			  input.filter("[value='" + val[i] + "']").each(function(){this.checked = true});
		  }
	  } else {  //其他表单选项直接设置值
		  input.val(value);
	  }
};


function ajax_post(form,istc,tcname,url){
		var targetform = $('.'+form);
		var cs = targetform.serialize();
		
		if(url == ''){
			url = targetform.attr('action');
		}
		
		if(targetform.hasClass('confirm')){
			var agbox = layer.confirm('您确定要删除吗？', {
				title:'提示信息',
				btn: ['确定','取消'] //按钮
			}, function(){
				post_data(url,cs,istc,tcname);
				layer.close(agbox);
			
			}, function(){
			  layer.close(agbox);
			});
			return false;	
		}else{
			post_data(url,cs,istc,tcname);	
		}
		
		return false;	
}

function post_data(url,cs,istc,tcname){
	$('.ajax-post-btn').attr('disabled',true);
	$.ajax({
		url:url,
		data:cs,
		type:'POST',
		success: function(data){
			if(data.code==1){
				layer.msg(data.msg);
				
					var timer = setInterval(function(){
						clearInterval(timer);
						if(data.url!=''){
							//alert(1);
							window.location.href=data.url;	
						}else{
							if(istc){
								//alert(2);
								//parent.iframe6.location.reload();
								var index = window.parent.layer.getFrameIndex(window.name);  
								window.parent.layer.close(index)
								window.parent.frames[tcname].location.reload();
							}else{
								//alert(3);
								window.location.reload();		
							}
							
						}
					},2000);	
					
			}else{
				layer.msg(data.msg);
				var timer = setInterval(function(){
					$('.ajax-post-btn').attr('disabled',false);	
					clearInterval(timer);
				},2000);	
			}
		}
	});	
}


function ajax_get(url,iscz){
	if(del_title != ''){
		var title = del_title;
	}else{
		var title = '您确定要删除吗？';	
	}
	
	var agbox = layer.confirm(title, {
				title:'提示信息',
				btn: ['确定','取消'] //按钮
	}, function(){
		get_data(url,iscz);
		layer.close(agbox);
			
	}, function(){
		layer.close(agbox);
	});
	return false;
}


function get_data(url,iscz){
	$.get(url,function(data){
		if(data.code==1){
			layer.msg(data.msg);
			setTimeout(function(){
				if (data.url!='') {
					location.href=data.url;
				}else if(iscz){
					location.reload();
				}else{
					
				}
			},2000);	
		}else{
			layer.msg(data.msg);
			var timer = setInterval(function(){
				clearInterval(timer);
			},2000);	
		}
	});	
	return false;
}


function conv_url(url){
	if(url.indexOf('?') > -1){
		return url+"&";	
	}else{
		return url+"?";		
	}
}


//获取手机验证码
var is_send = true;
function get_sms_code(the,yzmurl,type){	
	var $this = $(the);
	var tagname = $this.prop('tagName');
	if($this.parent().parent().find('.yzsjh').val()==''){	
		layer.msg('请输入手机号！');
		$this.parent().parent().find('.yzsjh').focus();
		return false;
	}
		
		
	if(is_send){
		$.ajax({
			url:yzmurl,
			data:{mobile:$this.parent().parent().find('.yzsjh').val(),type:type},
			type:'POST',
			success: function(data){
				if(data.code==1){
					layer.msg(data.msg);
					is_send = false;
					var s = 60;
					var timer = setInterval(function(){
						if(s<=0) {
							clearInterval(timer);
							if(tagname=='INPUT'){
								$this.val('重新获取');
							}else{
								$this.text('重新获取');		
							}
							
							is_send = true;
						}else{
							s --;
							if(tagname=='INPUT'){
								$this.val(s+'秒后重发');	
							}else{
								$this.text(s+'秒后重发');			
							}
							
						}
					},1000);	
				}else{
					layer.msg(data.msg);	
				}
			}
		});	
	}
		
	return false;	
}